import Vue from 'vue'
import VueRouter from 'vue-router'

import AdminView from "@/views/AdminView";
import LoginView from "@/views/LoginView";
import HomeView from "@/views/HomeView";
import Layout from "@/views/Layout";
import RegisterView from "@/views/RegisterView";
import BookView from "@/views/BookView";
import TypeView from "@/views/TypeView";
import NoticeView from "@/views/NoticeView";
import Person from "@/views/Person";
import LogView from "@/views/LogView";
import BorrowView from "@/views/BorrowView";

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: LoginView},
  {
    path: '/register',
    name: 'Register',
    component: RegisterView
  },
  {
    path: '/',
    name: 'Layout',
    redirect: '/admin',
    component: Layout,
    children: [ // 子路由
      {
        path: '',
        name: 'home',
        component: HomeView
      },
      {
        path: 'admin',
        name: 'admin',
        component: AdminView
      },
      {
        path: 'book',
        name: 'book',
        component: BookView
      },
      {
        path: 'type',
        name: 'type',
        component: TypeView
      },
      {
        path: 'notice',
        name: 'notice',
        component: NoticeView
      },
      {
        path: 'person',
        name: 'person',
        component: Person
      },
      {
        path: 'log',
        name: 'log',
        component: LogView
      },
      {
        path: 'borrow',
        name: 'borrow',
        component: BorrowView
      },
    ]
  },

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由守卫
router.beforeEach((to ,from, next) => {
  const user = localStorage.getItem("user");
  if (!user && to.path !== '/login' && to.path !== '/register') {
    return next("/login");
  }
  next();
})

export default router
